package com.faithbj.webservice.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.jws.WebService;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.faithbj.common.model.compare.AFMOfficalData;
import com.faithbj.common.utils.DateUtils;
import com.faithbj.service.CompareService;
import com.faithbj.support.vo.AFMCompareResponse;
import com.faithbj.support.vo.AFMCompareResponse.AFMData;
import com.faithbj.support.vo.CivilCompareResponse;
import com.faithbj.support.vo.CompareRequest;
import com.faithbj.support.vo.CompareResponse;
import com.faithbj.support.vo.HPCompareResponse;
import com.faithbj.support.vo.IBACompareResponse;
import com.faithbj.support.vo.PSCompareResponse;
import com.faithbj.support.vo.SICompareResponse;
import com.faithbj.support.vo.TaxCompareResponse;
import com.faithbj.webservice.WebServiceServer;

/**
 * webservice 实现类
 * 
 * @author chen liu
 */
//@WebService(endpointInterface = "com.faithbj.webservice.WebServiceServer")
//@Service("webServiceBean")
public class WebServiceServerTestImpl implements WebServiceServerTest {
    @Resource
    private CompareService compareService;

    @Override
    public String[] getArray(String[] paramArr) {
        System.out.println("********Server getArray()**********");
        for (String param : paramArr) {
            System.out.println(param);
        }
        System.out.println("********Server getArray()**********");
        return new String[] { "cat", "dog", "rat" };
    }

    @Override
    public List<User> getList(List<User> paramUserList) {
        System.out.println("********Server getList()**********");
        for (User user : paramUserList) {
            System.out.println(user.getName() + " : " + user.getAge());
            for (String key : user.getMap().keySet()) {
                System.out.println(key + "->" + user.getMap().get(key));
            }
            for (User child : user.getChildren()) {
                System.out.println(child);
            }
        }
        System.out.println("********Server getList()**********");

        List<User> returnList = new ArrayList<User>();
        for (int i = 0; i < 5; i++) {
            User user = new User();
            user.setName("liuchen" + i);
            user.setAge(i);
            Map<String, String> map = new HashMap<String, String>();
            user.setMap(map);
            map.put("cat" + i, "cat" + i);
            map.put("dog" + i, "dog" + i);
            List<User> list = new ArrayList<User>();
            user.setChildren(list);
            list.add(new User());
            list.add(new User());
            returnList.add(user);
        }

        return returnList;
    }

    @Override
    public Wraper getMap(Wraper paramWraper) {
        System.out.println("********Server getMap()**********");
        for (String key : paramWraper.getUserMap().keySet()) {
            System.out.println(key + "->" + paramWraper.getUserMap().get(key).getName());
            System.out.println(key + "->" + paramWraper.getUserMap().get(key).getAge());
        }
        System.out.println("********Server getMap()**********");

        HashMap<String, User> returnMap = new HashMap<String, User>();
        User user1 = new User();
        user1.setAge(12);
        user1.setName("liuchen");
        User user2 = new User();
        user2.setAge(24);
        user2.setName("chenliu");
        returnMap.put("1", user1);
        returnMap.put("2", user2);
        Wraper wraper = new Wraper();
        wraper.setUserMap(returnMap);
        return wraper;
    }

    @Override
    public boolean sendRequest(List<CompareRequest> requestList) {
        for (CompareRequest request : requestList) {
            System.out.println(request);
        }
        return true;
    }

    @Override
    public boolean sendResponse(List<AFMCompareResponse> responseList) {
        if (responseList != null) {
            // System.out.println(responseList.size());
            // for (CompareResponse response : responseList) {
            // // 调用对应的委办对象处理
            // if (response instanceof AFMCompareResponse) {
            // handleAFMResponse(response);
            // } else if (response instanceof CivilCompareResponse) {
            // handleCivilResponse(response);
            // } else if (response instanceof IBACompareResponse) {
            // handleIBAResponse(response);
            // } else if (response instanceof PSCompareResponse) {
            // handlePSResponse(response);
            // } else if (response instanceof SICompareResponse) {
            // handleSIResponse(response);
            // } else if (response instanceof HPCompareResponse) {
            // handleHPResponse(response);
            // } else if (response instanceof TaxCompareResponse) {
            // handleTaxResponse(response);
            // } else {
            // System.out.println("shit");
            // }
            // }
            for (AFMCompareResponse response : responseList) {
                handleAFMResponse(response);
            }
        }
        return true;
    }

    private void handleAFMResponse(CompareResponse response) {
        AFMCompareResponse afmResponse = (AFMCompareResponse) response;
        List<AFMOfficalData> updateList = compareService.getAFMOfficalDataForUpdate(afmResponse.getBatchNum(),
                afmResponse.getDepartmentNum(), afmResponse.getMainApplicantCardId());
        System.out.println(updateList.size());
        if (updateList != null) {
            for (AFMOfficalData data : updateList) {
                for (AFMData resData : afmResponse.getMemberDatas()) {
                    System.out.println(data.getApplicantCardNum());
                    System.out.println(resData.getApplicantCardNum());
                    if (data.getApplicantCardNum().equals(resData.getApplicantCardNum())) {
                        System.out.println(data.getApplicantCardNum());
                        BeanUtils.copyProperties(resData, data);
                        data.setIsQueryReturned(1);
                        data.setQueryReturnDt(DateUtils.getCurrentDate());
                        compareService.saveOrUpdateAFMOfficalData(data);
                        break;
                    }
                }
            }
        }
    }

    private void handleCivilResponse(CompareResponse response) {

    }

    private void handleIBAResponse(CompareResponse response) {

    }

    private void handlePSResponse(CompareResponse response) {

    }

    private void handleSIResponse(CompareResponse response) {

    }

    private void handleHPResponse(CompareResponse response) {

    }

    private void handleTaxResponse(CompareResponse response) {

    }
}
